package org.sdp.pearproject.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static Date add(Date tsp, int field, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(tsp);
        cal.add(field, amount);
        return cal.getTime();
    }

    public static String formatDatetime(Date tsp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(tsp);
    }

    public static String formatDate(Date tsp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(tsp);
    }

    public static Date parseDatetime(String tsp) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(tsp);
    }

    public static Date parseDate(String tsp) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(tsp);
    }

    public static Date addMonths(Date dateIn, Integer months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateIn);
        //得到前minute分钟的时间
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }

}
